package org.academic.texteditor.gui.form;

import java.awt.BorderLayout;

import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.academic.texteditor.gui.PrincipalWindow;
import org.academic.texteditor.gui.component.ComponentFactory;
import org.academic.texteditor.gui.component.JScroll;
import org.academic.texteditor.gui.component.PageContainer;
import org.academic.texteditor.gui.feature.Page;
import org.academic.texteditor.gui.helper.PageSize;
import org.academic.texteditor.gui.plugin.list.BulletPlugin;
import org.academic.texteditor.gui.plugin.list.NumberingPlugin;
import org.academic.texteditor.gui.plugin.reference.AuthorShortcutPlugin;
import org.academic.texteditor.gui.plugin.style.BoldPlugin;
import org.academic.texteditor.gui.plugin.style.ItalicPlugin;
import org.academic.texteditor.gui.plugin.style.UnderlinePlugin;
import org.academic.texteditor.gui.plugin.text.CloneTextDownPlugin;
import org.academic.texteditor.gui.plugin.text.CloneTextUpPlugin;
import org.academic.texteditor.gui.plugin.text.DeleteTextPlugin;
import org.academic.texteditor.gui.plugin.text.MarkTextPlugin;
import org.academic.texteditor.gui.plugin.text.MoveTextDownPlugin;
import org.academic.texteditor.gui.plugin.text.MoveTextUpPlugin;
import org.academic.texteditor.gui.tool.BorderDecoration;
import org.academic.texteditor.gui.tool.PageNumberDecoration;

public class PageContainerForm extends AbstractInternalFrame {

	private static final long serialVersionUID = 3113342256489317804L;

	private static int index = 0;

	private PageContainer pageContainer;

	public PageContainerForm() {
		super(msg.getMessage("form.pageContainer.title"), new BorderLayout());

		setName((++index) + "");

		addInternalFrameListener(new InternalFrameListener() {
			@Override
			public void internalFrameActivated(InternalFrameEvent e) {
				pageContainer.activate();
				PrincipalWindow.getInstance().setActualDocument((PageContainerForm) e.getInternalFrame());
			}

			@Override
			public void internalFrameClosed(InternalFrameEvent e) {

			}

			@Override
			public void internalFrameClosing(InternalFrameEvent e) {

			}

			@Override
			public void internalFrameDeactivated(InternalFrameEvent e) {
				pageContainer.deactivate();
				PrincipalWindow.getInstance().setActualDocument(null);
			}

			@Override
			public void internalFrameDeiconified(InternalFrameEvent e) {

			}

			@Override
			public void internalFrameIconified(InternalFrameEvent e) {

			}

			@Override
			public void internalFrameOpened(InternalFrameEvent e) {

			}
		});

		setSize(600, 400);
		setResizable(true);
	}

	@Override
	protected void createInterface() {
		JScroll scroll = ComponentFactory.getPageContent();
		add(scroll, BorderLayout.CENTER);

		pageContainer = scroll.getPageContainer();
		pageContainer.addPage(new Page(PageSize.A4));
		// pageContainer.addPage(new Page(PageSize.A4));

		PageNumberDecoration pageNumberDecoration = new PageNumberDecoration();
		pageNumberDecoration.setPageNumber(3);

		BorderDecoration borderDecoration = new BorderDecoration();

		for (Page page : pageContainer.getPages()) {
			page.addDecoration(pageNumberDecoration);
			page.addDecoration(borderDecoration);
			page.decore();
		}

		pageContainer.addPlugin(new CloneTextDownPlugin());
		pageContainer.addPlugin(new CloneTextUpPlugin());
		pageContainer.addPlugin(new DeleteTextPlugin());
		pageContainer.addPlugin(new MarkTextPlugin());
		pageContainer.addPlugin(new MoveTextUpPlugin());
		pageContainer.addPlugin(new MoveTextDownPlugin());
		pageContainer.addPlugin(new BulletPlugin());
		pageContainer.addPlugin(new NumberingPlugin());
		pageContainer.addPlugin(new BoldPlugin());
		pageContainer.addPlugin(new ItalicPlugin());
		pageContainer.addPlugin(new UnderlinePlugin());
		pageContainer.addPlugin(new AuthorShortcutPlugin());
		pageContainer.initialize();
	}

	public PageContainer getPageContainer() {
		return pageContainer;
	}

	@Override
	public String toString() {
		return super.getName();
	}

}
